/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import domain.Client;
import java.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.List;
import network.Network;

/**
 *
 * @author Jasper
 */
public class DrawController {
    private Client client;
    
    public Client getClient(){
        return client;
    }
    
    public DrawController(Client client) {
        this.client = client;
    } 
    
    //TCP --> snel verkeer
    public boolean guessWord(String naamRader, String woord){
        Socket link = null;
        boolean result = false;
        try{
            link = new Socket(InetAddress.getLocalHost(),Network.PORT);
            PrintWriter output = new PrintWriter(link.getOutputStream());
            BufferedReader input = new BufferedReader(new InputStreamReader(link.getInputStream()));
            
            //opdracht --> server
            output.println("guessWord");
            output.flush();
            
            //woord --> server
            output.println(woord);
            output.flush();
            
            //naam --> server
            output.println(naamRader);
            output.flush();
            //server --> resultaat
             result = Boolean.parseBoolean(input.readLine());
            
             
            //TODO indien gewonnen --> weergeven in gui + spel stoppen 
        }catch(IOException e){
            e.printStackTrace();
        }
        finally
        {
            try{
                link.setReuseAddress(true);  
            }catch(IOException e){
                System.exit(1);
            }
        }
        return result;
    }
    
    //TCP
    public void updateFrame(List<Point> points){
        Socket link = null;
        try{
            link = new Socket(InetAddress.getLocalHost(),Network.PORT);
            OutputStream outputstream = link.getOutputStream();      
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputstream);
            PrintWriter output = new PrintWriter(link.getOutputStream());
            output.println("updateFrame");
            output.flush();
            objectOutputStream.writeObject(points);
            objectOutputStream.flush();
        }catch(IOException e){
            e.printStackTrace();
        }
        finally
        {
            try{
                link.setReuseAddress(true);  
            }catch(IOException e){
                System.exit(1);
            }
        }
    }
    
    //TCP (?)
    public void setWord(String woord){
        Socket link = null;
        boolean result = false;
        try{
            link = new Socket(InetAddress.getLocalHost(),Network.PORT);
            PrintWriter output = new PrintWriter(link.getOutputStream());
            BufferedReader input = new BufferedReader(new InputStreamReader(link.getInputStream()));
            
            //woord --> server
            output.println(woord);
            output.flush();
            
            //server --> resultaat
            result = Boolean.parseBoolean(input.readLine());
            
            //TODO indien goed --> ok en speler kan raden
        }catch(IOException e){
            e.printStackTrace();
        }
        finally
        {
            try{
                link.setReuseAddress(true);  
            }catch(IOException e){
                System.exit(1);
            }
        }
    }

    public List<Point> loadPoints()  {
        Socket link = null;
        List<Point> points = null;
        try{
            link = new Socket(InetAddress.getLocalHost(),Network.PORT);
            InputStream inputStream = link.getInputStream();
            ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
            PrintWriter output = new PrintWriter(link.getOutputStream());
            output.println("loadPoints");
            output.flush();
            points = (List<Point>)objectInputStream.readObject();
        }catch(IOException e){
            e.printStackTrace();
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }
        finally{
            try{
                link.setReuseAddress(true);  
            }catch(IOException e){
                System.exit(1);
            }
        }
        
        return points;
    }

    public boolean isFinished() {
        Socket link = null;
        boolean result = false;
        try{
            link = new Socket(InetAddress.getLocalHost(),Network.PORT);
            BufferedReader input = new BufferedReader(new InputStreamReader(link.getInputStream()));
            PrintWriter output = new PrintWriter(link.getOutputStream());
            output.println("isFinished");
            output.flush();
            result = Boolean.parseBoolean(input.readLine());
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            try{
                link.setReuseAddress(true);  
            }catch(IOException e){
                System.exit(1);
            }
        }
        
        return result;
    }
}
